package org.example.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.Dao.PRViewDao;
import org.example.Dao.RPDao;
import org.example.Dao.URDao;
import org.example.Dao.UserDao;
import org.example.Pojo.PRView;
import org.example.Pojo.RP;
import org.example.Pojo.UR;
import org.example.Pojo.User;
import org.example.Service.PRViewService;
import org.example.Utils.UnionResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PRViewImpl implements PRViewService {
    @Resource
    private PRViewDao prViewDao;
    @Resource
    private RPDao rpDao;
    @Resource
    private URDao urDao;
    @Resource
    private UserDao userDao;
    @Override
    public UnionResult GetList() {
        QueryWrapper<PRView> queryWrapper = new QueryWrapper<>();
        return UnionResult.Success("",prViewDao.selectList(queryWrapper));
    }

    @Override
    public UnionResult Remove(String puid, String ruid) {
        QueryWrapper<RP> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("permission_id", puid);
        queryWrapper.eq("role_id", ruid);
        if (rpDao.delete(queryWrapper) > 0) {
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult add(String puid, String ruid) {
        RP rp = new RP();
        rp.setPermissionId(puid);
        rp.setRoleId(ruid);
        if (rpDao.insert(rp) > 0){
            return UnionResult.Success();
        }else{
            return UnionResult.Failure();
        }
    }

    @Override
    public UnionResult updateUR(String ruid, String uuid) {
        if (urDao.delete(new QueryWrapper<UR>().eq("user_id", uuid)) > 0) {
            UR ur = new UR();
            ur.setRoleId(ruid);
            ur.setUserId(uuid);
            if (urDao.insert(ur) > 0) {
                return UnionResult.Success();
            }else{
                return UnionResult.Failure();
            }
        }else{
            return UnionResult.Failure();
        }
    }
}
